package br.com.serialmanager.webservice;

import java.io.IOException;
import java.net.UnknownHostException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

public class HttpSoapRequest {
	
	private SoapObject soapObject;
	private Object response;
	private String url;
	
	public HttpSoapRequest(String namespace, String url, String action) {
		this.soapObject = new SoapObject(namespace, action);
		this.url = url;
	}
	
	public void addProperty(String name, String value) {
		this.soapObject.addProperty(name, value);
	}

	public void makeRequest() {
		try {
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(this.soapObject);
			
			HttpTransportSE httpTransport = new HttpTransportSE(this.url);
		
			httpTransport.call(null, envelope);
			this.response = envelope.getResponse();
		} catch (UnknownHostException e) {
			System.out.println("Não foi possível encontrar o host solicitado!");
		} catch (IOException e) {
			System.out.println("Erro: " + e.getMessage());
		} catch (XmlPullParserException e) {
			System.out.println("Erro: " + e.getMessage());
		}
	}
	
	public Object getResponse() {
		if (response == null) {
			response = "Não há resultados!";
		}
		return this.response;
	}
	
	public String getUrl() {
		return url;
	}
}
